home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / C Source ƒ / IC Misc Subs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-06  |  16.3 KB  |  635 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC Misc Subs.c
  3.     
  4. */
  5.  
  6. #include <stdarg.h>
  7. #include <Icons.h>
  8.  
  9. #include "IC Globals.h"
  10. #include "IC Misc Subs.h"
  11.  
  12. Str31 typed_chars;
  13. long typed_time;
  14. ListHandle typed_lh;
  15.  
  16. OSErr PrepMem(void){
  17.     long totalSize,contigSize;
  18.     
  19.     PurgeSpace(&totalSize,&contigSize);
  20.     
  21.     // totalSize is the total free space available if purge & compact took place
  22.     // contigSize is largest contiguous block available if purge took place
  23.     
  24.     PurgeMem(contigSize); // purge memory to get the contigSize area
  25.     
  26.     CompactMem(totalSize);
  27.     
  28.     return noErr;
  29. }
  30.  
  31. StringPtr SetPString(StringPtr str,short ct,...){
  32.     str[0]=0;
  33.     
  34.     if (ct)
  35.         vConcat(str,ct,__va(ct));
  36.     
  37.     return str;
  38. }
  39.  
  40. StringPtr Delete(StringPtr str,short start,short end){
  41.     
  42.     // keep values within bounds
  43.     if (start<1)
  44.         start=1;
  45.     
  46.     if (end>255)
  47.         end=255;
  48.     
  49.     if (str==(StringPtr)0)
  50.         return str; // return null string
  51.     
  52.     // if string shorter than start, nothing to delete
  53.     if (str[0]<start)
  54.         return str;
  55.     
  56.     // if string shorter than tail of delete, we're truncating string
  57.     if (str[0]<=end){
  58.         str[0]=start-1;
  59.         return str;
  60.     }
  61.     
  62.     if (start==1){
  63.         // move end down to front, string extends past end
  64.         str[0]-=end;
  65.         BlockMoveData((Ptr)&(str[end+1]),(Ptr)&(str[1]),str[0]);
  66.         return str;
  67.     }
  68.     
  69.     // start & end are in middle of  string
  70.     str[0] -= (end-start+1); // subtract chars being removed
  71.     BlockMoveData((Ptr)&(str[end+1]),(Ptr)&(str[start]),(end-start+1));
  72.     return str;
  73. }
  74.  
  75. StringPtr NewPString(StringPtr str){
  76.     StringPtr s;
  77.     
  78.     s=(StringPtr)NewPtr(256);
  79.     s[0]=0;
  80.     
  81.     if (str!=(StringPtr)0)
  82.         SetPString(s,1,str);
  83.     
  84.     return s;
  85. }
  86.  
  87. StringPtr Concat(StringPtr str,short ct,...){
  88.     return vConcat(str,ct,__va(ct));
  89. }
  90.  
  91. StringPtr vConcat(StringPtr str,short ct,va_list v){
  92.     StringPtr sp;
  93.     unsigned char mv;
  94.     
  95.     if (str==(StringPtr)0)
  96.         return str;
  97.     
  98.     while (ct){
  99.         sp=va_arg(v,StringPtr);
  100.         
  101.         if (sp!=(StringPtr)0){
  102.             mv=sp[0];
  103.             
  104.             if (mv+str[0]>255)
  105.                 mv=255-str[0];
  106.             
  107.             if (mv){
  108.                 BlockMoveData((Ptr)&(sp[1]),(Ptr)&(str[str[0]+1]),mv);
  109.                 str[0]+=mv;
  110.             }
  111.         }
  112.         ct--;
  113.     }
  114.     
  115.     return str;
  116. }
  117.  
  118. void InitMiscSubs(void){
  119.     typed_chars[0]=0;
  120.     typed_time=0;
  121.     typed_lh=(ListHandle)0;
  122. }
  123.  
  124. StringPtr TPCopy(StringPtr to,StringPtr source,short start,short count){
  125.     if (start<1){
  126.         count=count-(1-start);
  127.         start=1;
  128.     }
  129.     
  130.     if (start+count>source[0])
  131.         count=source[0]-start+1;
  132.     
  133.     if (count<0)
  134.         count=0;
  135.     to[0]=count;
  136.     if (count)
  137.         BlockMoveData((Ptr)&(source[start]),&(to[1]),count);
  138.     
  139.     return to;
  140. }
  141.  
  142. short TPPos(StringPtr sub,StringPtr str){
  143.     Handle sh=NewHandle(256);
  144.     long pos;
  145.     
  146.     if (sh==(Handle)0)
  147.         return -1;
  148.     
  149.     HLock(sh);
  150.     
  151.     BlockMoveData(str,*sh,str[0]+1);
  152.     
  153.     pos=Munger(sh,1,&(sub[1]),sub[0],(Ptr)0,0);
  154.     
  155.     HUnlock(sh);
  156.     DisposeHandle(sh);
  157.     
  158.     return pos;
  159. }
  160.  
  161. void StringToOSType(StringPtr s,OSType* otype){
  162.     Str255 ts;
  163.     
  164.     SetPString(ts,2,s,"\p\0\0\0\0");
  165.     BlockMoveData(&(ts[1]),otype,4);
  166. }
  167.  
  168. void OSTypeToString(OSType otype,StringPtr s){
  169.     BlockMoveData(&otype,&(s[1]),4);
  170.     s[0]=4;
  171. }
  172.  
  173. void DrawIcon(short id,Rect* r,Boolean highlighted){
  174.     Handle suite,iconh;
  175.     short transform;
  176.     
  177.     if ((System7)&&(GetIconSuite(&suite,id,svAllLargeData)==noErr)){
  178.         if (highlighted)
  179.             transform=ttSelected;
  180.         else
  181.             transform=ttNone;
  182.         
  183.         PlotIconSuite(r,0,transform,suite);
  184.         DisposeIconSuite(suite,false);
  185.     } else {
  186.         iconh=Get1Resource('ICN#',id);
  187.         if (iconh!=(Handle)0){
  188.             PlotIcon(r,iconh);
  189.             ReleaseResource(iconh);
  190.         }
  191.     }
  192. }
  193.  
  194. OSErr FSWriteQ(short refnum,long count,Ptr buf){
  195.     return FSWrite(refnum,&count,buf);
  196. }
  197.  
  198. OSErr FSReadQ(short refnum,long count,Ptr buf){
  199.     return FSRead(refnum,&count,buf);
  200. }
  201.  
  202. short SelectedLine(ListHandle lh){
  203.     Cell acell;
  204.     
  205.     SetPt(&acell,0,0);
  206.     
  207.     if (LGetSelect(true,&acell,lh))
  208.         return acell.v;
  209.     
  210.     return -1;
  211. }
  212.  
  213. Boolean IsKeyDown(short keycode){
  214.     unsigned char mykeys[16];
  215.     KeyMap* kmp;
  216.     
  217.     kmp=(KeyMap*)&mykeys;
  218.     
  219.     GetKeys(*kmp);
  220.     
  221.     return ((mykeys[keycode>>3]>>(keycode&7))&1);
  222. }
  223.  
  224. OSErr ICGetVolInfo(StringPtr name,short* vrn,short index,long* CrDate){
  225.     ParamBlockRec pb;
  226.     OSErr oe;
  227.     
  228.     if ((name[0]!=0)&&(name[name[0]]!=':'))
  229.         Concat(name,1,"\p:");
  230.     
  231.     pb.volumeParam.ioNamePtr=name;
  232.     pb.volumeParam.ioVRefNum=*vrn;
  233.     pb.volumeParam.ioVolIndex=index;
  234.     
  235.     oe=PBGetVInfoSync(&pb);
  236.     
  237.     if (oe==noErr){
  238.         *CrDate=pb.volumeParam.ioVCrDate;
  239.         *vrn=pb.volumeParam.ioVRefNum;
  240.     }
  241.     
  242.     return oe;
  243. }
  244.  
  245. OSErr MyGetAPPL(OSType sig,FSSpec* fs){
  246.     short i=1;
  247.     DTPBRec pbdt;
  248.     long crdate;
  249.     OSErr oe;
  250.     Boolean found=false;
  251.     
  252.     if (System7){
  253.         do {
  254.             fs->vRefNum=0;
  255.             fs->name[0]=0;
  256.             oe=ICGetVolInfo(fs->name,&(fs->vRefNum),i++,&crdate);
  257.             if (oe==noErr){
  258.                 fs->name[0]=0;
  259.                 pbdt.ioNamePtr=fs->name;
  260.                 pbdt.ioVRefNum=fs->vRefNum;
  261.                 
  262.                 oe=PBDTGetPath(&pbdt);
  263.                 
  264.                 if (oe==noErr){
  265.                     pbdt.ioIndex=0;
  266.                     pbdt.ioFileCreator=sig;
  267.                     
  268.                     oe=PBDTGetAPPLSync(&pbdt);
  269.                     
  270.                     found=(oe==noErr);
  271.                 }
  272.                 oe=noErr;
  273.             }
  274.         } while ((!found)&&(oe==noErr));
  275.     }
  276.     
  277.     if (found){
  278.         oe=noErr;
  279.         fs->parID=pbdt.ioAPPLParID;
  280.     } else {
  281.         oe=afpItemNotFound;
  282.         fs->vRefNum=0;
  283.         fs->parID=2;
  284.         fs->name[0]=0;
  285.     }
  286.     
  287.     return oe;
  288. }
  289.  
  290. OSErr ICFSpGetCatInfo(FSSpec* fs,short index,CInfoPBRec* pb){
  291.     pb->hFileInfo.ioVRefNum=fs->vRefNum;
  292.     pb->hFileInfo.ioDirID=fs->parID;
  293.     pb->hFileInfo.ioNamePtr=fs->name;
  294.     pb->hFileInfo.ioFDirIndex=index;
  295.     
  296.     return PBGetCatInfoSync(pb);
  297. }
  298.  
  299. OSErr ICFSpSetCatInfo(FSSpec* fs,CInfoPBRec* pb){
  300.     pb->hFileInfo.ioVRefNum=fs->vRefNum;
  301.     pb->hFileInfo.ioDirID=fs->parID;
  302.     pb->hFileInfo.ioNamePtr=fs->name;
  303.     
  304.     return PBSetCatInfoSync(pb);
  305. }
  306.  
  307. RgnHandle GetWindowContentRegion(WindowPtr wind){
  308.     return ((WindowPeek)wind)->contRgn;
  309. }
  310.  
  311. RgnHandle GetWindowStructureRegion(WindowPtr wind){
  312.     return ((WindowPeek)wind)->strucRgn;
  313. }
  314.  
  315. Boolean TitleBarOnScreen(WindowPtr wp){
  316.     RgnHandle rgn;
  317.     Boolean ret;
  318.     
  319.     rgn=NewRgn();
  320.     
  321.     CopyRgn(GetWindowStructureRegion(wp),rgn);
  322.     DiffRgn(rgn,GetWindowContentRegion(wp),rgn);
  323.     SectRgn(rgn,GetGrayRgn(),rgn);
  324.     ret=!EmptyRgn(rgn);
  325.     DisposeRgn(rgn);
  326.     
  327.     return ret;
  328. }
  329.  
  330. void GetWindowPortRect(WindowPtr wind,Rect* portRect){
  331.     *portRect=((WindowPeek)wind)->port.portRect;
  332. }
  333.  
  334. void GetWindowRect(WindowPtr wind,Rect* r){
  335.     SetPort(wind);
  336.     GetWindowPortRect(wind,r);
  337.     LocalToGlobal(&(topLeft(*r)));
  338.     LocalToGlobal(&(botRight(*r)));
  339. }
  340.  
  341. void CentreRect(Rect* inside_rect,Rect* res_rect){
  342.     Point stat_siz;
  343.     
  344.     stat_siz=botRight(*inside_rect);
  345.     SubPt(topLeft(*inside_rect),&stat_siz);
  346.     OffsetRect(res_rect,-res_rect->left,-res_rect->top);
  347.     SubPt(botRight(*res_rect),&stat_siz);
  348.     stat_siz.h/=2;
  349.     stat_siz.v/=2;
  350.     AddPt(topLeft(*inside_rect),&stat_siz);
  351.     OffsetRect(res_rect,stat_siz.h,stat_siz.v);
  352. }
  353.  
  354. void CentreAlert(short id){
  355.     AlertTHndl alerth;
  356.     Rect bounds;
  357.     
  358.     alerth=(AlertTHndl)GetResource('ALRT',id);
  359.     if (alerth!=(AlertTHndl)0){
  360.         bounds=qd.screenBits.bounds;
  361.         bounds.bottom=(bounds.bottom-bounds.top)*2/3+bounds.top;
  362.         HLock((Handle)alerth);
  363.         CentreRect(&bounds,&((*alerth)->boundsRect));
  364.         HUnlock((Handle)alerth);
  365.     }
  366. }
  367.  
  368. StringPtr GetAString(StringPtr dest,short id,short index){
  369.     GetIndString(dest,id,index);
  370.     return dest;
  371. }
  372.  
  373. void Assert(Boolean b){
  374.     if (!b)
  375.         DebugStr("\pAssertion failure ; sc");
  376. }
  377.  
  378. void SetItemEnable(MenuHandle mh,short item,Boolean enable){
  379.     if (enable)
  380.         EnableItem(mh,item);
  381.     else
  382.         DisableItem(mh,item);
  383. }
  384.  
  385. Boolean DirtyKey(char ch){
  386.     switch (ch){
  387.         case homeChar: casetentryname,list,c,n);
  388.         else {
  389.             n[0]=2;
  390.             n[1]=n[2]=255;
  391.         }
  392.         compa=IUCompString(s,n);
  393.         
  394.         if (((compa<0)||((compa==0)&&orequal))&&(IUCompString(n,best)<0)){
  395.             SetPString(best,1,n);
  396.             index=c.v;
  397.             good=true;
  398.         }
  399.     }
  400.     if (good)
  401.         LSetSingleSelection(list,index);
  402.     
  403.     return good;
  404. }
  405.  
  406. void DoListKey(ListHandle list,EventRecord* er,ListKeyUPP getentryname){
  407.     Cell c;
  408.     short index;
  409.     Boolean dummy,found;
  410.     long curticks=er->when;
  411.     char ch;
  412.     Str255 s;
  413.     
  414.     ch=er->message&0x00ff;
  415.     
  416.     if ((typed_lh!=list)||(ch<' ')){
  417.         typed_time=0;
  418.         typed_lh=list;
  419.     }
  420.     
  421.     switch (ch){
  422.         case downArrowChar:
  423.             c.h=c.v=index=0;
  424.             while (LGetSelect(true,&c,list)){
  425.                 c.v++;
  426.                 index=c.v;
  427.             }
  428.             if (index>(*list)->dataBounds.bottom)
  429.                 index=(*list)->dataBounds.bottom;
  430.             
  431.             LSetSingleSelection(list,index);
  432.             LAutoScroll(list);
  433.             break;
  434.         case upArrowChar:
  435.             c.h=c.v=0;
  436.             if (!LGetSelect(true,&c,list))
  437.                 c.v=(*list)->dataBounds.bottom;
  438.             if (c.v>0)
  439.                 c.v--;
  440.             LSetSingleSelection(list,c.v);
  441.             LAutoScroll(list);
  442.             break;
  443.         case homeChar:
  444.             LScroll(0,-(*list)->dataBounds.bottom,list);
  445.             break;
  446.         case endChar:
  447.             LScroll(0,(*list)->dataBounds.bottom,list);
  448.             break;
  449.         case pageUpChar:
  450.             LScroll(0,-((*list)->visible.bottom-(*list)->visible.top-2),list);
  451.             break;
  452.         case pageDownChar:
  453.             LScroll(0,((*list)->visible.bottom-(*list)->visible.top-2),list);
  454.             break;
  455.         case tabChar:
  456.             if (er->modifiers&shiftKey){
  457.                 found=false;
  458.                 if (LGetFirstSelection(list,&c,getentryname)){
  459.                     if (getentryname!=(ListKeyUPP)0)
  460.                         CallListKeyProc(getentryname,list,c,s);
  461.                     if (LSelectFirstBefore(list,s,getentryname))
  462.                         found=true;
  463.                 }
  464.                 if (!found)
  465.                     if ((!LGetLastSelection(list,&c,getentryname))||(!LSelectFirstAfter(list,LGetUniqueEntryName(list,&c,getentryname,s),getentryname,false))){
  466.                         LSelectFirstAfter(list,"\p",getentryname,false);
  467.                     }
  468.             }
  469.             break;
  470.         default:
  471.             if (ch>' '){
  472.                 if (curticks-typed_time>60)
  473.                     typed_chars[0]=0;
  474.                 typed_time=curticks;
  475.                 typed_chars[0]++;
  476.                 typed_chars[typed_chars[0]]=ch;
  477.                 if (!LSelectFirstAfter(list,typed_chars,getentryname,true)){
  478.                     Str31 t;
  479.                     t[0]=1;
  480.                     t[1]=255;
  481.                     LSelectFirstBefore(list,t,getentryname);
  482.                 }
  483.             }
  484.             break;
  485.     }
  486. }
  487.  
  488. StringPtr DecStr(long l,StringPtr s){
  489.     NumToString(l,s);
  490.     return s;
  491. }
  492.  
  493. StringPtr VersionStr(long lver,StringPtr s){
  494.     NumVersion ver;
  495.     Str255 tmp;
  496.     long maj;
  497.     
  498.     s[0]=0;
  499.     *((long*)&ver)=lver;
  500.     
  501.     if (ver.majorRev<=9)
  502.         maj=ver.majorRev;
  503.     else {
  504.         maj=(ver.majorRev >> 4)&0x0f;
  505.         maj*=10;
  506.         maj+=(ver.majorRev&0x0f);
  507.     }
  508.     DecStr(maj,s);Concat(s,1,"\p.");
  509.     
  510.     maj=(ver.minorAndBugRev>>4)&0x0f;
  511.     Concat(s,2,DecStr(maj,tmp),"\p.");
  512.     
  513.     maj=(ver.minorAndBugRev&0x0f);
  514.     Concat(s,1,DecStr(maj,tmp));
  515.     
  516.     // now the stage
  517.     if (ver.stage&0x20)
  518.         Concat(s,1,"\pd");
  519.     else if (ver.stage&0x40)
  520.         Concat(s,1,"\pa");
  521.     else if (ver.stage&0x60)
  522.         Concat(s,1,"\pb");
  523.     else if (ver.stage&0x80)
  524.         Concat(s,1,"\pr");
  525.     else
  526.         Concat(s,1,"\px");
  527.     
  528.     maj=ver.nonRelRev;
  529.     return Concat(s,1,DecStr(maj,tmp));
  530. }
  531.  
  532. OSErr GetDirName(FSSpec* fs){
  533.     CInfoPBRec pb;
  534.     
  535.     pb.hFileInfo.ioNamePtr=fs->name;
  536.     pb.hFileInfo.ioVRefNum=fs->vRefNum;
  537.     pb.hFileInfo.ioDirID=fs->parID;
  538.     pb.hFileInfo.ioFDirIndex=-1;// get information about ioDirID;
  539.     return PBGetCatInfoSync(&pb);
  540. }
  541.  
  542. OSErr CopyFork(short srn,short drn,long len){
  543.     OSErr err=noErr;
  544.     Ptr p=(Ptr)0;
  545.     long size=65536,count;
  546.     
  547.     do {
  548.         p=NewPtr(size);
  549.         if (p!=(Ptr)0)
  550.             size/=2;
  551.     } while ((p==(Ptr)0)&&(size>=512));
  552.     
  553.     if (p==(Ptr)0)
  554.         err=memFullErr;
  555.     
  556.     while ((err==noErr)&&(len>0)){
  557.         count=size;
  558.         if (count>len)
  559.             count=len;
  560.         
  561.         err=FSRead(srn,&count,p);
  562.         if ((err==noErr)&&(count==0))
  563.             err=eofErr;
  564.         if (err==noErr){
  565.             len -= count;
  566.             err=FSWrite(drn,&count,p);
  567.         }
  568.     }
  569.     
  570.     if (p!=(Ptr)0)
  571.         DisposePtr(p);
  572.     
  573.     return err;
  574. }
  575.  
  576. OSErr CopyFile(FSSpec* source,FSSpec* dest){
  577.     OSErr err,oerr;
  578.     CInfoPBRec pb;
  579.     short srrn,sdrn,drrn,ddrn;
  580.     
  581.     HDelete(dest->vRefNum,dest->parID,dest->name);
  582.     if ((err=ICFSpGetCatInfo(source,0,&pb))==noErr){
  583.         if ((err=HCreate(dest->vRefNum,dest->parID,dest->name,pb.hFileInfo.ioFlFndrInfo.fdCreator,pb.hFileInfo.ioFlFndrInfo.fdType))==noErr){
  584.             if ((err=HOpen(dest->vRefNum,dest->parID,dest->name,fsWrPerm,&ddrn))==noErr){
  585.                 if ((err=HOpenRF(dest->vRefNum,dest->parID,dest->name,fsWrPerm,&drrn))==noErr){
  586.                     if ((err=HOpen(source->vRefNum,source->parID,source->name,fsRdPerm,&sdrn))==noErr){
  587.                         if ((err=HOpenRF(source->vRefNum,source->parID,source->name,fsRdPerm,&srrn))==noErr){
  588.                             if ((err=CopyFork(sdrn,ddrn,pb.hFileInfo.ioFlLgLen))==noErr){
  589.                                 if ((err=CopyFork(srrn,drrn,pb.hFileInfo.ioFlRLgLen))==noErr){
  590.                                     // if no error copy complete
  591.                                 }
  592.                             }
  593.                             FSClose(srrn);
  594.                         }
  595.                         FSClose(sdrn);
  596.                     }
  597.                     FSClose(drrn);
  598.                 }
  599.                 FSClose(ddrn);
  600.             }
  601.         }
  602.     }
  603.     
  604.     if (err==noErr)
  605.         err=ICFSpSetCatInfo(dest,&pb);
  606.     if (err!=noErr)
  607.         HDelete(dest->vRefNum,dest->parID,dest->name);
  608.     
  609.     return err;
  610. }
  611.  
  612. StringPtr GetName(short id1,short id2,StringPtr name){
  613.     StringHandle sh;
  614.     
  615.     sh=GetString(id1);
  616.     if (sh==(StringHandle)0)
  617.         sh=GetString(id2);
  618.     
  619.     if (sh==(StringHandle)0)
  620.         SetPString(name,1,"\punnamed");
  621.     else
  622.         SetPString(name,1,*sh); // don't release the string handle, someone else may be using it
  623.     
  624.     return name;
  625. }
  626.  
  627. StringPtr GetOwnerName(StringPtr name){
  628.     return GetName(owner_id,machine_id,name);
  629. }
  630.  
  631. StringPtr GetMachineName(StringPtr name){
  632.     return GetName(machine_id,owner_id,name);
  633. }
  634.  
  635.